package org.jmq.myblog.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jmq.myblog.mapper.ArticleMapper;
import org.jmq.myblog.pojo.Article;
import org.jmq.myblog.pojo.MyPage;
import org.jmq.myblog.service.ArticleService;
import org.jmq.myblog.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired(required = false)
    private ArticleMapper articleMapper;

    @Override
    public ResponseResult<Map> search(MyPage page) {

        Integer p = page.getPage();
        Integer num = page.getRow();
        Integer start = (p-1)*num;
        String like = "%"+page.getSearch()+"%";

        int total = articleMapper.countArticle(like);
        List<Article> list = articleMapper.searchArticle(like,start,num);

        Map<String,Object> map = new HashMap<>();
        map.put("articleList",list);
        map.put("total",total);

        return new ResponseResult<Map>(200,"success",map);
    }

    @Override
    public ResponseResult<Map> getArticleList(MyPage page) {

        Page<Article> p = new Page<>(page.getPage(),page.getRow());

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.ne("delflag",1);
        wrapper.orderByDesc("publishtime");
        Page<Article> list = articleMapper.selectPage(p,wrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("articleList",list.getRecords());
        map.put("total",list.getTotal());

        return new ResponseResult<Map>(200,"success",map);
    }

    @Override
    public ResponseResult<List> allArticle() {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.orderByDesc("publishtime");
        List<Article> articleList = articleMapper.selectList(wrapper);
        return new ResponseResult<List>(200,"success",articleList);
    }

    @Override
    public ResponseResult addArticle(Article article) {

        int count = articleMapper.insert(article);
        if(count>0){
            return new ResponseResult(200,"success");
        }else {
            return new ResponseResult(200,"新增失败");
        }
    }

    @Override
    public ResponseResult modifyArticle(Article article) {
        int count = articleMapper.updateById(article);
        if(count>0){
            return new ResponseResult(200,"success");
        }else {
            return new ResponseResult(200,"修改失败");
        }
    }

    @Override
    public ResponseResult deleteArticle(int id) {
        Article article = articleMapper.selectById(id);
        article.setDelflag(1);
        int count = articleMapper.updateById(article);
        if(count>0){
            return new ResponseResult(200,"success");
        }else {
            return new ResponseResult(200,"删除失败");
        }
    }

    @Override
    public ResponseResult recoveryArticle(int id) {
        Article article = articleMapper.selectById(id);
        article.setDelflag(0);
        int count = articleMapper.updateById(article);
        if(count>0){
            return new ResponseResult(200,"success");
        }else {
            return new ResponseResult(200,"恢复失败");
        }
    }
}
